<template>
	<view>
		<u-navbar :is-back="true" :is-fixed="true" title="新增产品"></u-navbar>
		<scroll-view scroll-y scroll-with-animation style="height: calc(100vh - 110px);" :show-scrollbar="false">
			<view style=" background-color:#EFEDE9;">
				<view class="pl-2 pr-2 font-14 lineHight-40 font-weight-bold">服务信息</view>
			</view>
			<u-form :model="form" ref="uForm">
				<u-form-item label-width="230"  :label-style="labelStyle" label="产品/服务名称" prop="productServeName">
					<u-input v-model="form.productServeName" />
				</u-form-item>

				<u-form-item label-width="230" :label-style="labelStyle" label="服务商名称" prop="serviceName">
					<u-input v-model="form.serviceName" />
				</u-form-item>
				<u-form-item label-width="230" :label-style="labelStyle" label="服务介绍" prop="serviceIntroduction">
					<u-input type="textarea" height="50" v-model="form.serviceIntroduction" />
				</u-form-item>
				<u-form-item label-width="230" :label-style="labelStyle" label="￥" prop="servicePrice">
					<u-input v-model="form.servicePrice" />
				</u-form-item>
				<u-form-item label-width="230" :label-style="labelStyle" label="服务流程">
					<u-input type="textarea" height="50" v-model="form.serviceFlow" />
				</u-form-item>
				<u-form-item label-width="230" :label-style="labelStyle" label="售后服务">
					<u-input type="textarea" height="50" v-model="form.serviceInform" />
				</u-form-item>
				<view style=" background-color:#EFEDE9;">
					<view class="pl-2 pr-2 font-14 lineHight-40 font-weight-bold">上/下架状态</view>
				</view>
				<u-form-item label-width="230" :label-style="labelStyle" label="上/下架状态">
					<u-input v-model="form.productStatusText" type="select" :border="false" @click="showList = true" />
					<u-action-sheet :list="actionSheetList" v-model="showList"
						@click="actionSheetCallback"></u-action-sheet>
				</u-form-item>
			</u-form>
			<ferrButton title="确认新增产品" @click="addProduct()"></ferrButton>
		</scroll-view>
	</view>
</template>

<script>
	import $H from "@/common/free-lib/request.js";
	import $U from '@/common/free-lib/util.js';
	import ferrButton from "@/components/free-ui/ferr-button.vue";
	import calendar from "@/uni_modules/uni-calendar/components/uni-calendar/uni-calendar.vue"
	export default {
		components: {
			ferrButton,
			calendar
		},

		data() {
			return {
				rules: {
					productServeName: [{
							required: true,
							message: '产品/服务名称不能为空',
							trigger: ['change', 'blur'],
						}

					],
					serviceName: [{
						required: true,
						message: '服务商名称不能为空',
						trigger: ['change', 'blur'],
					}],
					serviceIntroduction: [{
						required: true,
						message: '服务介绍不能为空',
						trigger: ['change', 'blur'],
					}],
					servicePrice: [{
							required: true,
							message: '服务价格不能为空',
							trigger: ['change', 'blur'],
						},
						{
							required: true,

							type: 'number',
							message: '请输入数字',
						}
					],

				},
				form: {
					productServeName: "",
					serviceName: '',
					serviceIntroduction: '',
					servicePrice: '',
					serviceFlow: '',
					serviceInform: '',
					productStatus: '',
					productStatusText: '',
					updateTime: '',
				},
				value: '',
				showList: false,
				border: true,
				labelStyle: {
					paddingLeft: '13px'
				},
				actionSheetList: [{
						text: '上架',
						status: 1
					},
					{
						text: '下架',
						status: 0
					}
				],
			}
		},
		onReady() {
			this.form.productStatus = this.actionSheetList[0].status;
			this.form.productStatusText = this.actionSheetList[0].text;
			this.$refs.uForm.setRules(this.rules);
		},
		methods: {
			actionSheetCallback(index) {
				this.form.productStatus = this.actionSheetList[index].status;
				this.form.productStatusText = this.actionSheetList[index].text;
			},
			addProduct() {
				this.$refs.uForm.validate(valid => {
					if (valid) {
						uni.showLoading({
							title: '加载中'
						});

						const upperUserId = $U.getStorage("id");
						if (!upperUserId) {
							uni.showToast({
								title: "请重新登录",
								icon: "error",
							});
							uni.hideLoading();
							return;
						}

						$H.post(
							"/upper/upperMarketProduct/upper/add", {
								productServeName: this.form.productServeName,
								serviceName: this.form.serviceName,
								serviceIntroduction: this.form.serviceIntroduction,
								servicePrice: this.form.servicePrice,
								serviceFlow: this.form.serviceFlow,
								serviceInform: this.form.serviceInform,
								productStatus: this.form.productStatus,
								upperUserId: upperUserId,
							}, {}
						).then((res) => {
							if (res.success) {
								uni.showToast({
									title: "添加成功",
									icon: "none",
								});
								setTimeout(() => {
									uni.navigateBack({
										delta: 1
									});
								}, 1000);
							} else {
								uni.showToast({
									title: "添加失败",
									icon: "none",
								});
							}
							uni.hideLoading();
						}).catch(error => {
							console.error('请求出错', error); // 处理任何错误
							uni.showToast({
								title: "添加错误",
								icon: "none",
							});
							uni.hideLoading();
						})
					}else{
						console.log('验证失败');
					}
				});
			}
		}
	}
</script>

<style>
</style>